package com.cvit.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.model.Msg;
import com.cvit.model.Orders;
import com.cvit.service.OrdersService;
import com.cvit.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class OrdersController {

	@Autowired
	OrdersService ordersService;
	
	/**
	 * 查询所有
	 * */
	@RequestMapping("/getOrders")
	@ResponseBody
	public Msg getOrders(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 15);
		List<Orders> orders = ordersService.getAll();
		PageInfo<Orders> page = new PageInfo<Orders>(orders, 15);
		return Msg.success().add("Orders", page);
	}
	
	/**
	 * 根据身份证号或者学生号查询订单表
	 * */
	@RequestMapping("/getOrdersBy")
	@ResponseBody
	public Msg getOrdersbyId(@RequestParam(value="numberId") String numberId){
		Orders orders = ordersService.getOrdersById(numberId);
		if(Utils.isNull(orders)==false){
			return Msg.success().add("Orders", orders);
		}else
			return Msg.fail();
	}
	
	/**
	 * 根据学生Id查询订单表  不等于null=1
	 * */
	@RequestMapping("/getOrdersByStudentId")
	@ResponseBody
	public Msg getOrdersByStudentId(@RequestParam(value="StudentId")String id){
		int code = ordersService.getOrdersByStudentId(id);
		return Msg.success().add("code", code);
	}
	
	/**
	 * 添加订单
	 * */
	@RequestMapping("/addOrders")
	@ResponseBody
	public Msg addOrders(Orders orders){
		String id = ordersService.addOrders(orders);
		return Msg.success().add("id", id);
	}
	
//	/**
//	 * 修改订单
//	 * */
//	@RequestMapping("/updateOrders")
//	@ResponseBody
//	public Msg updateOrders(Orders orders){
//		ordersService.updateOrders(orders);
//		return Msg.success();
//	}
	
	/**
	 * 根据订单号查询订单状态
	 * */
	@RequestMapping("/getTypeById")
	@ResponseBody
	public Msg getTypeById(@RequestParam(value="orderId") String id){
		int orderType = ordersService.selectTypeById(id);
		return Msg.success().add("Type", orderType);
	}
	
	/**
	 *根据订单号查询订单
	 * */
	@RequestMapping("/getOrdersById")
	@ResponseBody
	public Msg getOrdersById(@RequestParam(value="orderId") String id){
		Orders orders = ordersService.getOrdersByOrdersId(id);
		return Msg.success().add("orders", orders);
	}
	
}
